package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        StudentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int A = sc.nextInt();
            switch (A) {
                case 1:
                    // System.out.println("添加学生");
                    addStudent();
                    break;
                case 2:
                    // System.out.println("删除学生");
                    removeStudent();
                    break;
                case 3:
                    // System.out.println("修改学生");
                    changeStudent();
                    break;
                case 4:
                    // System.out.println("查看学生");
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("退出");
                    break StudentLoop;
                default:
                    System.out.println("请输入正确选项");
                    break;
            }
        }
    }

    private void changeStudent() {
        System.out.println("请输入要修改的学生学号");
        String sid = sc.next();
        boolean flog = studentService.changeStudent(sid);
        if (flog) {
            System.out.println("修改成功");
        } else {
            System.out.println("学号不存在");
        }
    }

    private void removeStudent() {
        System.out.println("请输入要删除的学生学号");
        String sid = sc.next();
        boolean flog = studentService.removeStudent(sid);
        if (flog) {
            System.out.println("删除成功");
        } else {
            System.out.println("学号不存在");
        }
    }

    private void findAllStudent() {
        Student[] ArrayStudent = studentService.findAllStudent();
        if (ArrayStudent == null) {
            System.out.println("查无信息");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (Student student : ArrayStudent) {
            if (student != null) {
                System.out.println(student.getSid() + student.getName() + student.getAge() + student.getBirthday());
            }
        }
    }

    private void addStudent() {

        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入添加的学号");
            sid = sc.next();
            boolean flog = studentService.isExists(sid);
            if (flog) {
                System.out.println("学号重复");
            } else {
                break;
            }
        }
        System.out.println("请输入添加的姓名");
        String name = sc.next();
        System.out.println("请输入添加的年龄");
        String age = sc.next();
        System.out.println("请输入添加的生日");
        String birthday = sc.next();
        Student stn = new Student();
        stn.setSid(sid);
        stn.setName(name);
        stn.setAge(age);
        stn.setBirthday(birthday);
        boolean flog = studentService.addStudent(stn);
        if (flog) {
            System.out.println("添加成功");
        } else {
            System.out.println("学号重复或存储数量上限");
        }

    }
}
